# CMake4GDAL project is distributed under MIT license. See accompanying file LICENSE.txt.

set(FRMT_SOURCES gdalallregister.cpp)

get_cmake_property(_variableNames VARIABLES)
set(EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE "")
foreach (_variableName ${_variableNames})
    unset(MATCHED)
    string(REGEX MATCH "ADD_EXTERNAL_DEFERRED_PLUGIN_(.*)" MATCHED ${_variableName})
    if (NOT MATCHED)
        continue()
    endif()
    if (EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE STREQUAL "")
        set(EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE "// File generated by frmts/CMakeLists.txt. DO NOT EDIT !\nextern void DeclareExternalDeferredPlugins();\nvoid DeclareExternalDeferredPlugins()\n{\n")
    endif()
    string(APPEND EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE "    // Plugin ${CMAKE_MATCH_1}\n    extern void DeclareDeferred${CMAKE_MATCH_1}();\n    DeclareDeferred${CMAKE_MATCH_1}();\n")
    list(APPEND FRMT_SOURCES "${${_variableName}}")
endforeach()
if (NOT EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE STREQUAL "")
    string(APPEND EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE "}\n")
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/external_deferred_plugins.c" "${EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE}")
    list(APPEND FRMT_SOURCES "${CMAKE_CURRENT_BINARY_DIR}/external_deferred_plugins.c")
endif()

add_library(gdal_frmts OBJECT ${FRMT_SOURCES})
if (NOT EXTERNAL_DEFERRED_PLUGINS_SOURCE_CODE STREQUAL "")
    target_compile_definitions(gdal_frmts PRIVATE -DHAVE_EXTERNAL_DEFERRED_PLUGINS)
endif()

include(GdalDriverHelper)
include(CMakeDependentOption)

# JPEG must be defined before GTiff
gdal_dependent_format(jpeg "JPEG image format" "GDAL_USE_JPEG OR GDAL_USE_JPEG_INTERNAL")

# base driver provide frmt core functions
gdal_optional_format(raw "Raw formats:EOSAT FAST Format, FARSITE LCP and Vexcel MFF2 Image")
# JPEG must be defined before GTiff

# Exception to the rule: enable the GTiff driver by default, even if
# GDAL_BUILD_OPTIONAL_DRIVERS=OFF.
if (NOT DEFINED GDAL_ENABLE_DRIVER_GTIFF AND
    DEFINED GDAL_BUILD_OPTIONAL_DRIVERS AND
    NOT GDAL_BUILD_OPTIONAL_DRIVERS AND
    (GDAL_USE_TIFF OR GDAL_USE_TIFF_INTERNAL) AND
    (GDAL_USE_GEOTIFF OR GDAL_USE_GEOTIFF_INTERNAL))
    message(WARNING "Enabling GDAL_ENABLE_DRIVER_GTIFF=ON, despite GDAL_BUILD_OPTIONAL_DRIVERS=OFF. You can of course override this choice by setting GDAL_ENABLE_DRIVER_GTIFF=OFF")
    option(GDAL_ENABLE_DRIVER_GTIFF "Set ON to build GeoTIFF image format format" ON)
endif()

gdal_dependent_format(gtiff "GeoTIFF image format" "GDAL_USE_TIFF OR GDAL_USE_TIFF_INTERNAL;GDAL_USE_GEOTIFF OR GDAL_USE_GEOTIFF_INTERNAL")
set_package_properties(TIFF PROPERTIES PURPOSE "gdal_GTIFF: GeoTIFF image format")
gdal_format(mem "Read/write data in Memory")

# Exception to the rule: enable the VRT driver by default, even if
# GDAL_BUILD_OPTIONAL_DRIVERS=OFF.
if (NOT DEFINED GDAL_ENABLE_DRIVER_VRT AND
    DEFINED GDAL_BUILD_OPTIONAL_DRIVERS AND
    NOT GDAL_BUILD_OPTIONAL_DRIVERS)
    message(WARNING "Enabling GDAL_ENABLE_DRIVER_VRT=ON, despite GDAL_BUILD_OPTIONAL_DRIVERS=OFF. You can of course override this choice by setting GDAL_ENABLE_DRIVER_VRT=OFF")
    option(GDAL_ENABLE_DRIVER_VRT "Set ON to build Virtual GDAL Datasets" ON)
endif()

gdal_optional_format(vrt "Virtual GDAL Datasets")
if (NOT GDAL_ENABLE_DRIVER_VRT)
    # Even if we don't enable the driver (Open method), we still need to compile
    # Most of it
    add_subdirectory(vrt)
endif()

# Note: derived is derived of vrt
gdal_optional_format(derived "Derived datasets")

gdal_optional_format(gti "GDAL Tile Index")

# default formats

if (CMAKE_BUILD_TYPE MATCHES "Debug" OR GDAL_ENABLE_DRIVER_NULL)
  gdal_optional_format(null "NULL dummy driver")
endif ()

gdal_optional_format(libertiff "GeoTIFF support through libertiff library")
gdal_optional_format(hfa "Erdas Imagine .img")
gdal_optional_format(nitf "National Imagery Transmission Format")
gdal_optional_format(gxf "GXF")
gdal_optional_format(aaigrid "Arc/Info ASCII Grid Format.")
gdal_optional_format(ceos "CEOS translator")
gdal_optional_format(ceos2 "ASI CEOS translator" DRIVER_NAME_OPTION "SAR_CEOS")
gdal_optional_format(dted "Military Elevation Data")
gdal_optional_format(jdem "JDEM driver")
gdal_optional_format(envisat "Envisat")
gdal_optional_format(l1b "NOAA Polar Orbiter Level 1b Data Set (AVHRR)")
gdal_optional_format(rs2 "RS2 -- RadarSat 2 XML Product")
gdal_optional_format(ilwis "Raster Map")
gdal_optional_format(rmf "RMF --- Raster Matrix Format")
gdal_optional_format(leveller "Daylon Leveller heightfield")
gdal_optional_format(srtmhgt "SRTM HGT File Read Support")
gdal_optional_format(idrisi "Idrisi Raster Format")
gdal_optional_format(gsg "Implements the Golden Software Surfer 7 Binary Grid Format.")
gdal_optional_format(ers "ERMapper .ERS")
gdal_optional_format(jaxapalsar "JAXA PALSAR Level 1.1 and Level 1.5 processed products support")
gdal_optional_format(dimap "SPOT Dimap Driver")
gdal_optional_format(gff "Ground-based SAR Applitcations Testbed File Format driver")
gdal_optional_format(cosar "COSAR -- TerraSAR-X Complex SAR Data Product")
gdal_optional_format(pds "USGS Astrogeology ISIS Cube (Version 2)")
gdal_optional_format(adrg "ADRG reader and ASRP/USRP Reader")
gdal_optional_format(coasp "DRDC Configurable Airborne SAR Processor (COASP) data reader")
gdal_optional_format(tsx "TerraSAR-X XML Product Support")
gdal_optional_format(terragen "Terragen&trade; Terrain File")
gdal_optional_format(msgn "Meteosat Second Generation (MSG) Native Archive Format (.nat)")
gdal_optional_format(til "EarthWatch .TIL Driver")
gdal_optional_format(northwood "NWT_GRD/NWT_GRC -- Northwood/Vertical Mapper File Format")
gdal_optional_format(saga "SAGA GIS Binary Driver")
gdal_optional_format(xyz "ASCII Gridded XYZ")
include(avif/driver_declaration.cmake)
gdal_dependent_format(heif "HEIF" "GDAL_USE_HEIF")
gdal_optional_format(esric "ESRI compact cache")
gdal_optional_format(hf2 "HF2/HFZ heightfield raster")
gdal_optional_format(kmlsuperoverlay "")
gdal_optional_format(ctg "CTG driver")
gdal_optional_format(zmap "ZMAP")
gdal_optional_format(ngsgeoid "NOAA NGS Geoid Height Grids")
gdal_optional_format(iris "IRIS driver")
gdal_optional_format(map "OziExplorer .MAP")
gdal_optional_format(cals "CALS type 1")
set_package_properties(TIFF PROPERTIES PURPOSE "gdal_CALS: CALS type 1 driver")
gdal_optional_format(safe "SAFE -- Sentinel-1 SAFE XML Product")
gdal_optional_format(sentinel2 "Driver for Sentinel-2 Level-1B, Level-1C and Level-2A products.")
gdal_optional_format(prf "PHOTOMOD Raster File")
gdal_optional_format(mrf "Meta raster format")
gdal_optional_format(wmts "OGC Web Map Tile Service")
gdal_optional_format(grib "WMO General Regularly-distributed Information in Binary form")
gdal_optional_format(bmp "Microsoft Windows Device Independent Bitmap")
gdal_optional_format(tga "TGA")
gdal_optional_format(stacta "STACTA")
gdal_optional_format(snap_tiff "SNAP TIFF")

gdal_optional_format(bsb "Maptech/NOAA BSB Nautical Chart Format")
gdal_dependent_format(aigrid "Arc/Info Binary Grid Format" "OGR_ENABLE_DRIVER_AVC")
gdal_optional_format(usgsdem "USGS ASCII DEM (and CDED)")
gdal_optional_format(airsar "AirSAR Polarimetric Format")
gdal_optional_format(pcidsk "PCI Geomatics Database File")
gdal_optional_format(sigdem "Scaled Integer Gridded DEM .sigdem Driver")
gdal_dependent_format(msg "Meteosat Second Generation" "GDAL_USE_PUBLICDECOMPWT")
gdal_optional_format(rik "RIK -- Swedish Grid Maps")
gdal_optional_format(stacit "STACIT")
gdal_optional_format(pdf "Geospatial PDF") # write only if none of GDAL_USE_POPPLER, GDAL_USE_PODOFO or GDAL_USE_PDFIUM
                                           # is set
gdal_optional_format(rcm "Radarsat Constellation Mission")
gdal_optional_format(gdalg "GDAL Streamed Algorithm Format")

# formats with external library dependency
gdal_dependent_format(png "PNG image format" "GDAL_USE_PNG OR GDAL_USE_PNG_INTERNAL")
gdal_dependent_format(gif "Graphics Interchange Format" "GDAL_USE_GIF OR GDAL_USE_GIF_INTERNAL")

gdal_dependent_format(wcs "OGC Web Coverage Service" "GDAL_USE_CURL")
gdal_dependent_format(http "HTTP driver" "GDAL_USE_CURL")

gdal_dependent_format(netcdf "NetCDF network Common Data Form" "GDAL_USE_NETCDF")
gdal_optional_format(zarr "ZARR")
gdal_dependent_format(daas "Airbus DS Intelligence Data As A Service(DAAS)" "GDAL_USE_CURL")
gdal_dependent_format(eeda "Earth Engine Data API" "GDAL_USE_CURL")
gdal_dependent_format(fits "FITS Driver" "GDAL_USE_CFITSIO")
gdal_dependent_format(hdf5 "Hierarchical Data Format Release 5 (HDF5)" "GDAL_USE_HDF5")
gdal_dependent_format(plmosaic "PLMosaic (Planet Labs Mosaics API)" "GDAL_USE_CURL")
gdal_dependent_format(wms "Web Map Services" "GDAL_USE_CURL")
gdal_dependent_format(ogcapi "OGCAPI" "GDAL_USE_CURL")
gdal_dependent_format(gta "Generic Tagged Arrays" "GDAL_USE_GTA")
gdal_dependent_format(webp "WebP" "GDAL_USE_WEBP")
gdal_dependent_format(hdf4 "Hierarchical Data Format Release 4 (HDF4)" "GDAL_USE_HDF4")
gdal_dependent_format(mbtiles "MBTile" "GDAL_USE_SQLITE3;OGR_ENABLE_DRIVER_GPKG;OGR_ENABLE_DRIVER_MVT")
gdal_dependent_format(postgisraster "PostGIS Raster driver" "GDAL_USE_POSTGRESQL")
gdal_dependent_format(dds "DirectDraw Surface" "GDAL_USE_CRNLIB")
gdal_dependent_format(kea "Kea" "GDAL_USE_KEA;GDAL_USE_HDF5")
include(openjpeg/driver_declaration.cmake)
include(tiledb/driver_declaration.cmake)
gdal_dependent_format(exr "EXR support via OpenEXR library" "GDAL_USE_OPENEXR")
gdal_dependent_format(pcraster "PCRaster CSF 2.0 raster file driver" "GDAL_USE_LIBCSF OR GDAL_USE_LIBCSF_INTERNAL")
gdal_dependent_format(jpegxl "JPEG-XL" "GDAL_USE_JXL")
gdal_dependent_format(basisu_ktx2 "Basis Universal and KTX2 texture formats" "GDAL_USE_BASISU")
if( NOT WORDS_BIGENDIAN )
    gdal_optional_format(miramon "MiraMonRaster")
endif()


# ######################################################################################################################
# driver with proprietary libraries Kakadu software SDK
include(jp2kak/driver_declaration.cmake)
gdal_dependent_format(jpipkak "JPIP Streaming" "GDAL_USE_KDU")
# LizardTech's decoding software development kit (DSDK)
include(mrsid/driver_declaration.cmake)
include(georaster/driver_declaration.cmake)
include(ecw/driver_declaration.cmake)

# Register all frmt drivers according to FRMT_<driver> flags
gdal_standard_includes(gdal_frmts)
add_dependencies(gdal_frmts generate_gdal_version_h)
if (ENABLE_GNM)
  target_compile_definitions(gdal_frmts PRIVATE -DGNM_ENABLED)
  target_include_directories(gdal_frmts PRIVATE $<TARGET_PROPERTY:gnm_frmts,SOURCE_DIR>)
endif ()
target_include_directories(
  gdal_frmts PRIVATE $<TARGET_PROPERTY:gdal_vrt,SOURCE_DIR> $<TARGET_PROPERTY:ogrsf_generic,SOURCE_DIR>)
target_sources(${GDAL_LIB_TARGET_NAME} PRIVATE $<TARGET_OBJECTS:gdal_frmts>)
set_property(TARGET gdal_frmts PROPERTY POSITION_INDEPENDENT_CODE ${GDAL_OBJECT_LIBRARIES_POSITION_INDEPENDENT_CODE})
